unit loginform;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, BCButton, Forms, Controls, Graphics, Dialogs,
  ExtCtrls, StdCtrls, IniFiles, LCLType;

type

  { TfrmLogin }

  TfrmLogin = class(TForm)
    BCButton1: TBCButton;
    BCButton2: TBCButton;
    ComboBox1: TComboBox;
    Edit1: TEdit;
    Edit2: TEdit;
    Image2: TImage;
    Label1: TLabel;
    Label2: TLabel;
    procedure BCButton1Click(Sender: TObject);
    procedure BCButton2Click(Sender: TObject);
    procedure ComboBox1Select(Sender: TObject);
    procedure Edit2KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
    procedure FormShow(Sender: TObject);
  private

  public
    Selected:string;
    dbhost,dbname,dbuser,dbpwd:string;
    dbport:integer;
  end;

var
  frmLogin: TfrmLogin;

implementation

uses
  datamodule;

{$R *.frm}

{ TfrmLogin }

procedure TfrmLogin.FormShow(Sender: TObject);
var
  mIni2:TIniFile;
  mIndex:integer;
begin
  //填充工作库
  with dm.pubqry do
  begin
    Close;
    Sql.Clear;
    Sql.Add('select * from users order by dbcaption asc');
    Open;
    First;
    while not Eof do
    begin
      ComboBox1.Items.Add(FieldByName('dbcaption').AsString);
      Next;
    end;
  end;
  mIni2:=TIniFile.Create(Extractfilepath(Application.ExeName)+'login.ini');
  Selected:=mIni2.ReadString('common','lastselect','');
  Edit1.Text:=mIni2.ReadString('common','lastuser','');
  mIni2.Free;
  if ComboBox1.Items.Count>0 then
  begin
    if Selected<>'' then
    begin
      mIndex:=ComboBox1.Items.IndexOf(Selected);
      if mIndex<>-1 then
        ComboBox1.ItemIndex:=mIndex;
    end
    else
      ComboBox1.ItemIndex:=0;
  end;
  if Trim(Edit1.Text)<>'' then
     Edit2.SetFocus;
end;

procedure TfrmLogin.FormCloseQuery(Sender: TObject; var CanClose: boolean);
var
  mIni:TIniFile;
begin
  if ModalResult=mrOK then
  begin
    //保存上次选择的数据库和用户名
    mIni:=TIniFile.Create(ExtractFilepath(Application.ExeName)+'login.ini');
    mIni.WriteString('common','lastselect',Trim(ComboBox1.Text));
    mIni.WriteString('common','lastuser',Trim(Edit1.Text));
    mIni.Free;
  end;
end;

procedure TfrmLogin.BCButton1Click(Sender: TObject);
begin
  if Trim(Edit1.Text)='' then
  begin
    MessageDlg('请输入用户名。',mtInformation,[mbOK],0);
    Edit1.SetFocus;
    Exit;
  end;
  if Trim(Edit2.Text)='' then
  begin
    MessageDlg('请输入登录密码。',mtInformation,[mbOK],0);
    Edit2.SetFocus;
    Exit;
  end;
  if Trim(ComboBox1.Text)='' then
  begin
    MessageDlg('请选择工作库。',mtInformation,[mbOK],0);
    ComboBox1.SetFocus;
    Exit;
  end;
  //检验用户名和密码，并返回对应的工作库连接信息
  with dm.pubqry do
  begin
    Close;
    Sql.Clear;
    Sql.Add('select * from users where uname=:uname and upwd=MD5(:upwd) and dbcaption=:dbcaption');
    Params.ParamByName('uname').AsString:=Trim(Edit1.Text);
    Params.ParamByName('upwd').AsString:=Trim(Edit2.Text);
    Params.ParamByName('dbcaption').AsString:=ComboBox1.Text;
    Open;
    First;
    if not Eof then
    begin
      //读取参数
      dbhost:=FieldByName('dbhost').AsString;
      dbname:=FieldByName('dbname').AsString;
      dbuser:=FieldByName('dbuser').AsString;
      dbpwd:=FieldByName('dbpwd').AsString;
      dbport:=FieldByName('dbport').AsInteger;
      dm.isAdmin:=FieldByName('isadmin').AsInteger;
    end
    else
    begin
      //登录失败
      MessageDlg('登录失败，请检查工作库、用户名和密码后重新登录。',mtError,[mbOK],0);
      Exit;
    end;
  end;
  ModalResult:=mrOK;
end;

procedure TfrmLogin.BCButton2Click(Sender: TObject);
begin
  ModalResult:=mrCancel;
end;

procedure TfrmLogin.ComboBox1Select(Sender: TObject);
begin
  Edit1.Text:=Copy(ComboBox1.Text,1,Pos('工作库',ComboBox1.Text)-1);
end;

procedure TfrmLogin.Edit2KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_RETURN then
  begin
    if Trim(Edit2.Text)<>'' then
    begin
      BCButton1.OnClick(nil);
    end;
  end;
end;


end.

